//
//  WJPhotoManager.m
//  MyNewProject
//
//  Created by 王春辉 on 2020/1/4.
//  Copyright © 2020年 sun. All rights reserved.
//

#import "WJPhotoManager.h"
#import "STPhotoKitController.h"
#import "UIImagePickerController+ST.h"

@interface WJPhotoManager()<UIImagePickerControllerDelegate, STPhotoKitDelegate, UINavigationControllerDelegate>

@property (nonatomic, strong) UIImage *image;
@property (nonatomic, copy) void(^completeHolder)(UIImage *image);
@property (nonatomic, assign) CGSize imageSize;
@property (nonatomic, weak) __kindof UIViewController *viewController;
@end
static WJPhotoManager *manager = nil;
@implementation WJPhotoManager
+ (WJPhotoManager *)manager {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[WJPhotoManager alloc] init];
    });
    return manager;
}

/**
 选取照片
 
 @param viewController viewController
 @param completeHandler 回调
 */
- (void)choosePhotoWithViewController:(__kindof UIViewController *)viewController Complete:(void(^)(UIImage *))completeHandler {
    self.viewController = viewController;
    self.completeHolder = completeHandler;
    [self startChoosePhoto];
}

/**
 选取照片 （指定尺寸）
 
 @param viewController viewController
 @param imageSize 照片大小
 @param completeHandler 回调
 */
- (void)choosePhotoWithViewController:(__kindof UIViewController *)viewController AndSize:(CGSize)imageSize Complete:(void (^)(UIImage * _Nonnull))completeHandler {
    self.viewController = viewController;
    self.completeHolder = completeHandler;
    self.imageSize = imageSize;
    [self startChoosePhoto];
}

- (void)startChoosePhoto {
    UIAlertController *alertController = [[UIAlertController alloc]init];
    UIAlertAction *action0 = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerController *controller = [UIImagePickerController imagePickerControllerWithSourceType:UIImagePickerControllerSourceTypeCamera];
        if ([controller isAvailableCamera] && [controller isSupportTakingPhotos]) {
            [controller setDelegate:self];
            [self.viewController presentViewController:controller animated:YES completion:nil];
        }else {
            NSLog(@"%s %@", __FUNCTION__, @"相机权限受限");
        }
    }];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"从相册获取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerController *controller = [UIImagePickerController imagePickerControllerWithSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
        [controller setDelegate:self];
        if ([controller isAvailablePhotoLibrary]) {
            [self.viewController presentViewController:controller animated:YES completion:nil];
        }
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alertController addAction:action0];
    [alertController addAction:action1];
    [alertController addAction:action2];
    [self.viewController presentViewController:alertController animated:YES completion:nil];
}



#pragma mark -- mark STPhotoKitDelegate

- (void)photoKitController:(STPhotoKitController *)photoKitController resultImage:(UIImage *)resultImage {
    self.image = resultImage;
    if (self.completeHolder) {
        self.completeHolder(self.image); }
}

#pragma mark -- mark UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
    [picker dismissViewControllerAnimated:true completion:^{
        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
        STPhotoKitController *vc = [[STPhotoKitController alloc] init];
        vc.delegate = self;
        vc.imageOriginal = image;
        if (!CGSizeEqualToSize(self.imageSize, CGSizeZero)) {
            vc.sizeClip = self.imageSize; }
        [self.viewController presentViewController:vc animated:true completion:nil];
    }];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:true completion:nil];
}
@end
